* Keep the IEs
use "$data\01_main\folk_flown_merge_eventready.dta", clear
gen event_ie_dmy=0
replace event_ie_dmy=1 if event_ie!=.
bysort shnro: egen newiesometime = max(event_ie_dmy)
keep if newiesometime==1
* Get the firm ids
preserve
* FLOWN: 2006 - 2019 fine
use "W:\Inequality\2021analysis\Data\Omistusdata\flown_20032019_divs_owners_1st_attemp.dta", clear
bysort shnro vuosi: gen nr_of_firms = _N
egen NR_of_firms=max(nr_of_firms), by(shnro vuosi)
keep syrtun shnro vuosi NR_of_firms ownrshp
tab NR_of_firms
gen ie=1
gen ie_mltpl=0
replace ie_mltpl=1 if NR_of_firms>1
ren NR_of_firms nr_of_firms
label var nr_of_firms "Number of firms a person owns in a given year"
label var ie "Incorporated entrepreneur"
label var ie_mltpl "Incorporated entrepreneur who owns more than one firm"
keep if vuosi>2005
save "$data\99_temp\flown.dta", replace
* Yrtti: extend the IE info to early years, meaning years 1997-2005
use "D:\d37\custom-made\shareholder_panel_1997_2016_muok.dta" , clear
keep if verovuosi<2006
ren verovuosi vuosi
bysort shnro vuosi: gen nr_of_firms = _N
egen NR_of_firms=max(nr_of_firms), by(shnro vuosi)
ren syrtun_maktun syrtun
keep syrtun shnro vuosi NR_of_firms
tab NR_of_firms
gen ie=1
gen ie_mltpl=0
replace ie_mltpl=1 if NR_of_firms>1
ren NR_of_firms nr_of_firms
keep shnro syrtun vuosi nr_of_firms ie ie_mltpl
label var nr_of_firms "Number of firms a person owns in a given year"
label var ie "Incorporated entrepreneur"
label var ie_mltpl "Incorporated entrepreneur who owns more than one firm"
keep shnro syrtun vuosi nr_of_firms ie ie_mltpl
gen yrtti=1
drop if shnro==""
append using "$data\99_temp\flown.dta"
replace yrtti=0 if yrtti==.
sort syrtun shnro vuosi
order syrtun shnro vuosi
egen firm_ind=group(syrtun shnro)
tsset firm_ind vuosi
tsfill
foreach var of varlist syrtun shnro nr_of_firms ie ie_mltpl yrtti{
bys firm_ind: carryforward `var', gen(`var'n) 
drop `var'
ren `var'n `var'
}
* keep the first firm that the owner has
sort shnro vuosi
bys shnro: gen eka=_n
keep if eka==1
drop firm_ind eka 
rename syrtun syrtun_ownrshp
keep shnro vuosi syrtun_ownrshp
save "$data\99_temp\firmdummies.dta", replace
restore
merge 1:1 shnro vuosi using "$data\99_temp\firmdummies.dta"
drop if _merge==2
drop _merge
erase "$data\99_temp\firmdummies.dta"
erase "$data\99_temp\flown.dta"
rename syrtun_ownrshp syrtun
* Save the shnro-syrtun links
preserve
keep if syrtun!=""
keep shnro syrtun vuosi
duplicates drop syrtun vuosi, force
save "$data\99_temp\shnrosyrtunlinks.dta", replace
restore




* Take the firm data
preserve
use "D:\ready-made\FIRM_FSS\Tilinpaatospaneeli\tp8620.dta", clear
drop if syrtun==""
destring vuosi, replace
merge 1:1 syrtun vuosi using "$data\99_temp\shnrosyrtunlinks.dta", keep(1 3)
erase "$data\99_temp\shnrosyrtunlinks.dta"
gen newie=0
replace newie=1 if _merge==3
bysort syrtun: egen newiesometime = max(newie)
keep if newiesometime==1
rename tplv sales
replace sales=0 if sales==.
keep if sales>0
sort syrtun vuosi
bysort syrtun: gen nr = _n
drop if nr==1 & _merge==1
keep syrtun vuosi shnro newie
* Calculate the number of years the firms survive, already dropped those with 0 turnover
bysort syrtun: gen nobs = _N
collapse nobs, by(syrtun)
rename nobs firmspells
save "$data\99_temp\firmspells.dta", replace
restore
merge m:1 syrtun using "$data\99_temp\firmspells.dta"
erase "$data\99_temp\firmspells.dta"
drop _merge
gen surv5yr0=0
replace surv5yr=1 if firmspells>5 & firmspells!=.
bysort shnro: egen surv5yr = max(surv5yr)
bysort shnro: egen firmlifetime = max(firmspells)
* Merge the industry codes
preserve
use vuosi syrtun tol08_2 using "D:\ready-made\FIRM_ENTER\yritystol8819.dta", replace
drop if vuosi<1995
save "$data\99_temp\tol.dta", replace
restore
merge m:1 syrtun vuosi using "$data\99_temp\tol.dta"
drop if _merge==2
drop _merge
destring tol08_2, replace force
bysort shnro: egen tol08 = max(tol08_2)
drop tol08_2
sort shnro vuosi
erase "$data\99_temp\tol.dta"
keep shnro vuosi syrtun surv5yr firmlifetime tol08
* Fill firm ids forward
gen syrtun_filled = syrtun
gsort shnro vuosi
bysort shnro (vuosi): gen byte fillstart = !missing(syrtun_filled)
bysort shnro (vuosi): replace fillstart = fillstart[_n-1] if _n > 1 & fillstart[_n-1] == 1
bysort shnro (vuosi): replace syrtun_filled = syrtun_filled[_n-1] if fillstart == 1 & missing(syrtun_filled)
replace syrtun = syrtun_filled if missing(syrtun) & fillstart == 1
drop syrtun_filled fillstart
drop if syrtun==""
* Merge balance sheet data
preserve
use syrtun vuosi tplv yrhenk palkat soskul elakekul jal omapo using "D:\ready-made\FIRM_FSS\Tilinpaatospaneeli\tp8620.dta", clear
destring vuosi, replace
drop if syrtun==""
save "$data\99_temp\fss.dta", replace
restore
merge m:1 syrtun vuosi using "$data\99_temp\fss.dta", keep(1 3)
drop _merge
erase "$data\99_temp\fss.dta"

* Rename, label and save
label var syrtun "Firm id"
label var surv5yr "1 if firm survives for 5 years (files taxes and positive sales)"
label var firmlifetime "Firm age, capped to final year"
	rename tol08 firm_ind
label var firm_ind "Firm industry (NACE 2-digit)"
	rename tplv sales
	rename yrhenk empl
	gen stfc=palkat+soskul+elakekul
	rename jal va
	gen prod = va / empl
	rename omapo eq
	order shnro - empl stfc va prod eq
	keep shnro - eq
label var sales "Sales (turnover)"
label var empl "Number of employees, full-time equivalent"
label var stfc "Sum of wages, other personnel expenses and pension expenses"
label var va "Value added = revised operating margin + wages + other personnel expenses"
label var prod "Value added / number of employees"
label var eq "Equity"
save "$data\01_main\folk_flown_merge_eventready_iespec.dta", replace
